<?php
    if (! class_exists('NaturalExtension_Bookshelf')) {
        class NaturalExtension_Bookshelf {
            private $utils,
                    $books;

            public function __construct() {
                $this->utils = new NaturalExtension_Utils();

                $this->books = $this->utils->get_bookshelf_items();

                $this->load_scripts_and_styles();
            }

            public function load_scripts_and_styles() {
                $this->utils->nq_scripts(
                    array('natext-bookshelf-Bookshelf', 'natext_bookshelf_Bookshelf', '', $this->utils->config['version']),
                    array('natext-bookshelf-Editor', 'natext_bookshelf_Editor', '', $this->utils->config['version']),
                    array('natext-bookshelf', 'natext_bookshelf', array('jquery', 'editor', 'natext'), $this->utils->config['version'])
                );
            }

            public function display_books() {
                $items = array();

                if (sizeof($this->books) > 0) {
                    foreach ($this->books as $book) {
                        $thumbnail = has_post_thumbnail($book->ID) ? get_the_post_thumbnail($book->ID, 'medium') : '<img class="attachment-medium wp-post-image" alt="dummy cover" title="no cover attached" src="' . OF_DIRECTORY . '/patch/images/dummy-book-cover.jpg">';

                        $items[] = sprintf('<li data-book-id="%s" title="Click to edit">%s<span class="close" title="Remove this book">&times;</span></li>', $book->ID, $thumbnail);

                    }
                }

                echo implode('', $items);
            }
        }
    }
?>